package com.dhq.myapp.util

import android.content.Context
import android.os.Environment
import com.rokid.cxr.client.extend.CxrApi
import com.rokid.cxr.client.extend.callbacks.PhotoPathCallback
import com.rokid.cxr.client.extend.callbacks.SendStatusCallback
import com.rokid.cxr.client.extend.callbacks.SyncStatusCallback
import com.rokid.cxr.client.extend.callbacks.UnsyncNumResultCallback
import com.rokid.cxr.client.utils.ValueUtil
import java.io.File


/**
 * DESC
 * Created by DHQ on 2025/10/11.
 */
class GlassOptionUtil(var mContext: Context) {


    /**
     * set photo params
     *
     * @param width photo width
     * @param height photo height
     * @return set status
     * @see ValueUtil.CxrStatus
     * @see ValueUtil.CxrStatus.REQUEST_SUCCEED request succeed
     * @see ValueUtil.CxrStatus.REQUEST_WAITING request waiting, do not request again
     * @see ValueUtil.CxrStatus.REQUEST_FAILED request failed
     */
    fun setPhotoParams(width: Int, height: Int): ValueUtil.CxrStatus{
        return CxrApi.getInstance().setPhotoParams(width, height)
    }


    // photo path callback
    private val photoPathResult = object : PhotoPathCallback {
        /**
         * photo path callback
         *
         * @param status photo path status
         * @see ValueUtil.CxrStatus
         * @see ValueUtil.CxrStatus.RESPONSE_SUCCEED response succeed
         * @see ValueUtil.CxrStatus.RESPONSE_INVALID response invalid
         * @see ValueUtil.CxrStatus.RESPONSE_TIMEOUT response timeout
         * @param path photo path
         */
        override fun onPhotoPath(
            status: ValueUtil.CxrStatus?,
            path: String?
        ) {

        }

    }

    /**
     * take photo path
     *
     * @param width photo width
     * @param height photo height
     * @param quality photo quality
     *
     * @return take photo path result
     * @see ValueUtil.CxrStatus
     * @see ValueUtil.CxrStatus.REQUEST_SUCCEED request succeed
     * @see ValueUtil.CxrStatus.REQUEST_WAITING request waiting, do not request again
     * @see ValueUtil.CxrStatus.REQUEST_FAILED request failed
     */
    fun takePhotoPath(width: Int, height: Int, quality: Int): ValueUtil.CxrStatus? {
        return CxrApi.getInstance().takeGlassPhoto(width, height, quality, photoPathResult)
    }

    /**
     * 拍照
     */
    fun takePhoto(photoResult:PhotoPathCallback): ValueUtil.CxrStatus? {
        return CxrApi.getInstance().takeGlassPhoto(320, 240, 100, photoResult)
    }

    /**
     * [4032x3024, 4000x3000, 4032x2268, 3264x2448, 3200x2400,
     * 2268x3024, 2876x2156, 2688x2016, 2582x1936, 2400x1800,
     * 1800x2400, 2560x1440, 2400x1350, 2048x1536, 2016x1512,
     * 1920x1080, 1600x1200, 1440x1080, 1280x720, 720x1280,
     * 1024x768, 800x600, 648x648, 854x480, 800x480, 640x480,
     * 480x640, 352x288, 320x240, 320x180, 176x144]
     */
    fun takePhoto(width: Int, height: Int, quality: Int,photoResult:PhotoPathCallback): ValueUtil.CxrStatus? {
        return CxrApi.getInstance().takeGlassPhoto(width, height, quality, photoResult)
    }



    val streamCallback = object : SendStatusCallback {
        /**
         * send succeed
         */
        override fun onSendSucceed() {

        }

        /**
         * send failed
         * @param errorCode
         * @see ValueUtil.CxrSendErrorCode
         */
        override fun onSendFailed(errorCode: ValueUtil.CxrSendErrorCode?) {
            TODO("Not yet implemented")
        }

    }

    /**
     * send stream
     * @param type
     * @see ValueUtil.CxrStreamType
     * @see ValueUtil.CxrStreamType.WORD_TIPS teleprompter words
     * @param stream
     * @param fileName
     * @return send status
     * @see ValueUtil.CxrStatus
     * @see ValueUtil.CxrStatus.REQUEST_SUCCEED request succeed
     * @see ValueUtil.CxrStatus.REQUEST_WAITING request waiting, do not request again
     * @see ValueUtil.CxrStatus.REQUEST_FAILED request failed
     */
    fun sendStream(type: ValueUtil.CxrStreamType, stream: ByteArray, fileName: String): ValueUtil.CxrStatus? {
        return CxrApi.getInstance().sendStream(type, stream, fileName, streamCallback)
    }



    // get unsync num
    private val unSyncCallback = object : UnsyncNumResultCallback {
        /**
         * get un sync num result
         * @param status
         * @see ValueUtil.CxrStatus
         * @see ValueUtil.CxrStatus.RESPONSE_SUCCEED response succeed
         * @see ValueUtil.CxrStatus.RESPONSE_INVALID response invalid
         * @see ValueUtil.CxrStatus.RESPONSE_TIMEOUT response timeout
         * @param audioNum
         * @param pictureNum
         * @param videoNum
         */
        override fun onUnsyncNumResult(
            status: ValueUtil.CxrStatus?,
            audioNum: Int,
            pictureNum: Int,
            videoNum: Int
        ) {

        }

    }

    /**
     * get unsync num
     *
     * @return get unsync num status
     * @see ValueUtil.CxrStatus
     * @see ValueUtil.CxrStatus.REQUEST_SUCCEED request succeed
     * @see ValueUtil.CxrStatus.REQUEST_WAITING request waiting, do not request again
     * @see ValueUtil.CxrStatus.REQUEST_FAILED request failed
     */
    fun getUnsyncNum(): ValueUtil.CxrStatus? {
        return CxrApi.getInstance().getUnsyncNum(unSyncCallback)
    }

    /**
     * 根据文件路径获取眼镜文件
     */
    fun getFile(path: String,listener:SyncListener){
        // sync status callback
        val syncCallback = object : SyncStatusCallback {
            /**
             * sync start
             */
            override fun onSyncStart() {
                listener.onSyncStart()
            }

            /**
             * sync single file
             * @param fileName file name which sync success
             */
            override fun onSingleFileSynced(fileName: String?) {
                listener.onSingleFileSynced(fileName)

            }

            /**
             * sync failed
             */
            override fun onSyncFailed() {

                listener.onSyncFailed()
            }

            /**
             * sync finished
             */
            override fun onSyncFinished() {

                listener.onSyncFinished()
            }

        }

        val ficturesDir: File? = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        //同步图片文件
//        val result = CxrApi.getInstance().syncSingleFile(ficturesDir?.absolutePath,
//            ValueUtil.CxrMediaType.PICTURE,FileUtil.getFileName(path), syncCallback)

        val result = CxrApi.getInstance().syncSingleFile(ficturesDir?.absolutePath,
            ValueUtil.CxrMediaType.PICTURE,path, syncCallback)

        LogUtil.e("同步结果",result.toString())

        /**
         * start sync
         *
         * @param svaePaht save path
         * @param types sync types
         * @return sync status true: sync succeed, false: sync failed
         */
        fun startSync(svaePaht: String, types: Array<ValueUtil.CxrMediaType>): Boolean {
            return CxrApi.getInstance().startSync(svaePaht, types, syncCallback)
        }
    }


    /**
     * 根据文件路径获取眼镜文件
     */
    fun getFileList(path: String,listener:SyncListener){
        // sync status callback
        val syncCallback = object : SyncStatusCallback {
            /**
             * sync start
             */
            override fun onSyncStart() {
                listener.onSyncStart()
            }

            /**
             * sync single file
             * @param fileName file name which sync success
             */
            override fun onSingleFileSynced(fileName: String?) {
                listener.onSingleFileSynced(fileName)

            }

            /**
             * sync failed
             */
            override fun onSyncFailed() {

                listener.onSyncFailed()
            }

            /**
             * sync finished
             */
            override fun onSyncFinished() {

                listener.onSyncFinished()
            }

        }

        //同步图片文件
//        val result = CxrApi.getInstance().syncSingleFile(path,
//            ValueUtil.CxrMediaType.PICTURE,FileUtil.getFileName(path), syncCallback)
//
//        LogUtil.e("同步结果",result)

        val ficturesDir: File? = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES)

        val result = CxrApi.getInstance().startSync(ficturesDir?.absolutePath, arrayOf(ValueUtil.CxrMediaType.PICTURE), syncCallback)
        LogUtil.e("同步结果",result)

        /**
         * start sync
         *
         * @param svaePaht save path
         * @param types sync types
         * @return sync status true: sync succeed, false: sync failed
         */
        fun startSync(svaePaht: String, types: Array<ValueUtil.CxrMediaType>): Boolean {
            return CxrApi.getInstance().startSync(svaePaht, types, syncCallback)
        }
    }

    interface SyncListener{
        fun onSyncStart()
        fun onSingleFileSynced(fileName: String?)
        fun onSyncFailed()
        fun onSyncFinished()
    }

}